package _16_hash_table;

import org.junit.Test;

/**
 * @author: mornd
 * @dateTime: 2023/6/20 - 1:35
 */
public class MyTest {

    @Test
    public void test() {
        HashTable ht = new HashTable();
        ht.put(0, "1", null);
        ht.put(16, "2", null);
        ht.put(0, "3", null);

        ht.remove(0, "3");

        ht.printTableStructure();
    }

    @Test
    public void testResize() {
        HashTable ht = new HashTable(4);
        ht.put(0, "1", null);
        ht.put(0, "2", null);
        ht.printTableStructure();
        ht.put(0, "3", null);
        ht.put(4, "4", null);
        ht.printTableStructure();
        ht.put(0, "5", null);
        ht.put(0, "6", null);
        ht.put(0, "7", null);
        ht.put(8, "8", null);
        ht.printTableStructure();
    }

    @Test
    public void testResize2() {
        HashTable ht = new HashTable(2);
        ht.put(0, "1", null);
        ht.put(2, "2", null);
        ht.put(4, "3", null);
        ht.put(8, "4", null);
    }

    @Test
    public void testStrHash() {
        String s1 = "abc";
        System.out.println(s1.hashCode());

        int hash = 0;
        for (int i = 0; i < s1.length(); i++) {
//            hash = hash * 31 + s1.charAt(i);
            hash = (hash << 5) - hash + s1.charAt(i);
        }
        System.out.println(hash);
    }

    @Test // 测试hash分部
    public void testHash() {
        HashTable ht = new HashTable();
        for (int i = 0; i < 200000; i++) {
            Object t = new Object();
            ht.put(t, null);
        }
        System.out.println(ht.classify());
    }

    @Test
    public void test2() {
        HashTable hashTable = new HashTable();
        hashTable.put(2, null);
        hashTable.put(524290, null);

        hashTable.printTableStructure();
    }
}
